﻿Imports System.Threading
Public Class Main
    Private _killerBytes As KillerBytesOld
    Private _folderService As FolderService
    Private _cliente As Cliente
    Private _hilo As Thread

    Private Sub btnLoadFile_Click(sender As Object, e As EventArgs) Handles btnLoadFile.Click
        Dim result As DialogResult = OpenFileDialog.ShowDialog()
        If result = DialogResult.OK Then
            _killerBytes = New KillerBytesOld()
            AddHandler _killerBytes.Verbose, AddressOf Escribe
            _killerBytes.KillBytes(OpenFileDialog.FileName)
            Escribe("Terminado!")
        End If
    End Sub

    Private Sub Escribe(ByVal mensaje As String)
        If lblConsole.InvokeRequired Then
            lblConsole.Invoke(New MethodInvoker(Sub()
                                                    lblConsole.Text = lblConsole.Text & vbNewLine & mensaje
                                                    lblConsole.SelectionStart = lblConsole.TextLength
                                                    lblConsole.ScrollToCaret()
                                                    Refresh()
                                                End Sub))
        End If

    End Sub

    Private Sub Main_FormClosing(sender As Object, e As Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        While _hilo.IsAlive
            Try
                _hilo.Abort()
            Catch ex As Exception
            End Try
        End While
    End Sub

    Private Sub Main_Load(sender As Object, e As EventArgs) Handles Me.Load

        'Dim processor As New Processor
        'processor.Name = "WinRar"
        'processor.Executable = New FileSearcher("rar.exe", {"C:\Program Files\WinRAR"})
        'processor.Definitions.Add(New Definition(ProcessingMode.Decompressor, "%exec% x -y %in% %out%", Output.Folder, New List(Of String)(New String() {".rar"})))
        'processor.Definitions.Add(New Definition(ProcessingMode.Packer, "%exec% a -m0 %out% %int%", Output.File))
        'processor.OutputFormat = ".rar"
        'processor.pause = True

        'Dim killerBytes As New KillerBytes
        'killerBytes.Processors.Add(processor)
        'killerBytes.Process("C:\input\Umlet.rar")


        _folderService = New FolderService
        AddHandler _folderService.RarListo, AddressOf folderService_RARLISTO
        AddHandler _folderService.Dormir, AddressOf folderservice_Dormir
        _hilo = New Thread(AddressOf _folderService.Empezar)
        _hilo.Start()
    End Sub

    Private Sub folderService_RARLISTO(ByVal sender As Object, ByVal e As EventArgs)
        _killerBytes = New KillerBytesOld()
        AddHandler _killerBytes.Verbose, AddressOf Escribe
        _killerBytes.KillBytes(TryCast(sender, FolderService).Carpeta)
        Escribe("Terminado!")

        _cliente = New Cliente()
        _cliente.IP = "127.0.0.1"
        _cliente.Puerto = "8080"
        _cliente.Conectar()
        _cliente.EnviarDatos("Terminado")
        _cliente.Desconectar()
    End Sub

    Private Shared Sub folderservice_Dormir(ByVal sender As Object, ByVal e As EventArgs)
        Thread.Sleep(1000)
    End Sub
End Class
